package cn.zd.demo1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import cn.zd.demo1.util.KeyboardUtil;

public class EdittextActivity extends AppCompatActivity implements View.OnClickListener, View.OnFocusChangeListener {

    private TextView tv_1;
    private Button btn_1;
    private EditText et_0;
    private EditText et_1;
    private EditText et_2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Toast.makeText(this,this.getClass().getName(),Toast.LENGTH_LONG).show();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edittext);
        tv_1 = findViewById(R.id.tv_1);
        btn_1 = findViewById(R.id.btn_1);
        et_0 = findViewById(R.id.et_0);
        et_1 = findViewById(R.id.et_1);
        et_2 = findViewById(R.id.et_2);
        btn_1.setOnClickListener(this);
        et_1.setOnFocusChangeListener(this);
        et_2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.toString().length() == 6) {
                    KeyboardUtil.hideInputMethod(EdittextActivity.this, et_1);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        tv_1.setText(et_1.getText().toString());
        Toast.makeText(this,"焦点给输入框",Toast.LENGTH_SHORT);
        et_1.requestFocus();
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        et_0.setText(et_1.getText().toString());
    }
}